package com.system.controller;

import com.system.Tools.UserTools;
import com.system.function.object.Supplier;
import com.system.function.object.User;
import com.system.function.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserTools userTools;



  /*  //用户个人主页
    @RequestMapping("/userView")
    public ModelAndView userView(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("/user/userIndex");
        User userMessageForSeesion = userTools.getUserMessageForSeesion(request);
        User user = userService.getUser(userMessageForSeesion);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(user);
        mv.addObject("userList",user);
        return mv;
    }*/


    //用户登录主页
    @RequestMapping("/loginView")
    public ModelAndView toIndex() {
        ModelAndView mv = new ModelAndView("/user/userLogin");
        return mv;
    }


    //用户登录
    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request, User user) {
        User userSearch = userService.getUser(user);
        if (null != userSearch && null != userSearch && userSearch.getPw().equals(user.getPw())) {
            request.getSession().setAttribute("users", userSearch);
            return "true-admin";
        }
        return "false";
    }

    //用户退出登录
    @RequestMapping("/outSignIn")
    public ModelAndView outSignIn(HttpServletRequest request) {
        request.getSession().setAttribute("users", null);
        ModelAndView mv = new ModelAndView("/index");
        return mv;

    }

    //用户查询
    @RequestMapping("/s")
    @ResponseBody
    public Map<String, Object> s(HttpServletRequest request,User user) {
        User userMessageForSeesion = userTools.getUserMessageForSeesion(request);
        //非超级管理员 不能统计管理员数据
        if(!userMessageForSeesion.getLv().equals("0")){
            user.setLv("1");
        }

        List<User> allUser = userService.getAllUser(user);
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("total", userService.getCountUser(user));
        returnMap.put("rows", allUser);
        return returnMap;
    }

    //用户添加
    @RequestMapping("/add")
    @ResponseBody
    public String add(User user) {
        if(null == user.getLv()){
            user.setLv("1");
        }
        return userService.addUser(user);
    }

    //用户修改
    @RequestMapping("/up")
    @ResponseBody
    public String up(User user) {
        return userService.updateUser(user);
    }

    //用户删除
    @RequestMapping("/del")
    @ResponseBody
    public String del(User user) {
        return userService.delUser(user);
    }

    //查询
    @RequestMapping("/select")
    @ResponseBody
    public List<User> select(HttpServletRequest request,User user) {
        List<User> allSupplier = new ArrayList<>();
        User user1 = new User();
        user1.setName("超级管理员");
        user1.setLv("0");
        User user2 = new User();
        user2.setName("普通管理员");
        user2.setLv("1");

        User userMessageForSeesion = userTools.getUserMessageForSeesion(request);
        if(userMessageForSeesion.getLv().equals("0")){
            allSupplier.add(user1);
        }
        allSupplier.add(user2);
        return allSupplier;
    }
}
